<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="所属部门" prop="deptId">
        <el-select
            v-model="queryParams.deptId"
            placeholder="请选择所属部门"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="d in deptList"
              :key="d.deptId"
              :label="d.deptName"
              :value="d.deptId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="患者名称" prop="patientName">
        <el-input
            v-model="queryParams.patientName"
            placeholder="请输入患者名称"
            clearable
            size="small"
            style="width:200px"/>
      </el-form-item>
      <el-form-item label="挂号类型" prop="schedulingType">
        <el-select
            v-model="queryParams.schedulingType"
            placeholder="可用状态"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in schedulingTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时段" prop="subsectionType">
        <el-select
            v-model="queryParams.subsectionType"
            placeholder="挂号时段"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in subsectionTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号状态" prop="regStatus">
        <el-select
            v-model="queryParams.registrationStatus"
            placeholder="挂号状态"
            clearable
            size="small"
            style="width:200px">
          <el-option
              v-for="dict in regStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时间" prop="visitDate">
        <el-date-picker
            v-model="queryParams.visitDate"
            size="small"
            style="width:200px"
            value-format="yyyy-MM-dd"
            type="date"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="normal" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="regTableList">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="right" inline class="demo-table-expand">
            <el-form-item label="挂号ID:">
              <span>{{ props.row.registrationId }}</span>
            </el-form-item>
            <!--二维码支付模块-->
            <div id="qrcode" style="margin-top: 20px; margin-left: 30%"></div>
            <el-form-item label="挂号员:">
              <span>{{ props.row.createBy }}</span>
            </el-form-item>
            <el-form-item label="创建时间:">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patientName"/>
      <el-table-column label="挂号科室" align="center" prop="deptId" :formatter="deptIdFormatter"/>
      <el-table-column label="接诊医生" align="center" prop="doctorName"/>
      <el-table-column label="挂号费用" align="center" prop="registrationAmount">
        <template slot-scope="scope">
          <span>{{ scope.row.registrationAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流水编号" align="center" prop="registrationNumber"/>
      <el-table-column label="状态" align="center" prop="registrationStatus" :formatter="regStatusFormatter"/>
      <el-table-column label="就诊日期" align="center" prop="visitDate"/>
      <el-table-column label="挂号类型" align="center" prop="schedulingType" :formatter="schedulingTypeFormatter"/>
      <el-table-column label="挂号时段" align="center" prop="subsectionType" :formatter="subsectionTypeFormatter"/>
      <el-table-column label="操作" align="center" width="200">
        <!--挂号状态  0未收费,1待就诊，2,就诊中，3，就诊完成，4，已退号，5 作废-->
        <template slot-scope="scope">
          <el-button v-if="scope.row.registrationStatus==='0'" type="success" icon="el-icon-check" size="mini"
                     @click="handleCollect(scope.row)">收费
          </el-button>
          <el-button v-if="scope.row.registrationStatus==='1'" type="danger" icon="el-icon-close" size="mini"
                     @click="handleReturn(scope.row)">退号
          </el-button>
          <el-button v-if="scope.row.registrationStatus==='0'" type="danger" icon="el-icon-check" size="mini"
                     @click="handleInvalid(scope.row)">作废
          </el-button>
          <el-tag v-if="scope.row.registrationStatus==='2'">就诊中</el-tag>
          <el-tag v-if="scope.row.registrationStatus==='3'" type="success">就诊完成</el-tag>
          <el-tag v-if="scope.row.registrationStatus==='4'" type="warning">已退号</el-tag>
          <el-tag v-if="scope.row.registrationStatus==='5'" type="danger">作废</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->

  </div>
</template>

<script>
import QRCode from "qrcodejs2";
export default {
  name: "RegisteredList",
  data() {
    return {
      //微信支付返回的数据
      payData: {
        msg: "",
        code: "",
        QrCode:"",  //二维码
        out_trade_no:""  ////商户平台订单号（非微信平台订单号）
      },
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 挂号数据
      regTableList: [],
      // 部门数据
      deptList: [],
      // 用户数据
      userList: [],
      // 挂号单状态数据字典
      regStatusOptions: [],
      // 门诊急诊
      schedulingTypeOptions: [],
      // 上午  中午  晚上
      subsectionTypeOptions: [],
      // 查询参数
      queryParams: {
        patientName: undefined,
        deptId: undefined,
        schedulingType: undefined,
        subsectionType: undefined,
        // visitDate: new Date(),
        // queryDate: new Date(),
        registrationStatus: undefined
      }
    }
  },
  created() {
    // 查询挂号列表--部门信息
    this.init();
    // 挂号单状态字典数据
    this.getDictDataAPI1();
    // 门诊急诊字典数据
    this.getDictDataAPI2();
    // 上午  中午  晚上字典数据
    this.getDictDataAPI3();
  },
  methods: {
    init() {
      this.listRegistration()
      //查询所有的部门表
      this.$http.get("/hisRemoteDictData/getAllDept").then(res=>{
        // console.log(res.data)
        this.deptList = res.data.data
        // console.log(this.deptList)
      })
    },
    // 搜索
    handleQuery() {
      this.listRegistration()
    },
    resetQuery() {
      // this.queryParams = {}
      // this.listRegistration()
      //页面刷新
      location.reload();
    },
    // 查询挂号列表
    listRegistration() {
      this.$http.post('/hisRegistration/getAllRegistration', this.queryParams).then(res => {
        //console.log(res.data.data)
        this.regTableList = res.data.data
      })
    },
    // 翻译部门-->部门名称(当前行的部门id与请求过来部门列表里面的id相同就把部门名称给它)
    deptIdFormatter(row, column) {
      let deptName
      this.deptList.filter(item => {
        if (item.deptId === row.deptId) {
          deptName = item.deptName
        }
      })
      return deptName;
    },
    // 翻译排班类型
    getDictDataAPI2(){
      this.$http.get("/hisRemoteDictData/queryByDictType/"+'his_scheduling_type').then(res => {
        this.schedulingTypeOptions = res.data
      })
    },
    schedulingTypeFormatter(row, column) {
      let schedulingTypeLabel ;
      this.schedulingTypeOptions.filter(item => {
        if (parseInt(item.dictValue) === parseInt(row.schedulingType)) {
          schedulingTypeLabel = item.dictLabel
        }
      })
      return schedulingTypeLabel;
    },
    // 翻译排班时段
    getDictDataAPI3(){
      this.$http.get("/hisRemoteDictData/queryByDictType/"+'his_subsection_type').then(res => {
        this.subsectionTypeOptions = res.data
      })
    },
    subsectionTypeFormatter(row, column) {
      let subsectionTypeLabel = "上午";
      this.subsectionTypeOptions.filter(item => {
        if (parseInt(item.dictValue) === parseInt(row.subsectionType)) {
          subsectionTypeLabel = item.dictLabel
        }
      })
      return subsectionTypeLabel;
    },
    // 翻译挂号单类型
    getDictDataAPI1(){
      this.$http.get("/hisRemoteDictData/queryByDictType/"+'his_registration_status').then(res => {
        this.regStatusOptions = res.data
      })
    },
    regStatusFormatter(row, column) {
       let regStatusLabel = "待收费";
        this.regStatusOptions.filter(item => {
          if (parseInt(item.dictValue) === parseInt(row.registrationStatus)) {
            regStatusLabel = item.dictLabel
          }
        })
        return regStatusLabel;
    },

    //让二维码显示


    // 收费
    handleCollect(row) {
      this.$http.get("/hisRegistration/updateStatus/"+row.registrationId+"/"+"1").then(res => {
        if (res.data.code === 200) {
          this.$message.success("收费成功")
          this.listRegistration()
        } else {
          this.$message.error("收费失败")
        }
      })
      // this.$http.get("/weChatPay/wechatPayController/"+"挂号收费6元"+"/"+"0.01").then(res => {
      //   this.payData=res.data;
      //   console.log(this.payData)
      //   // console.log(res)
      //   if (this.payData.msg === "预下单成功") {
      //     let qrcode = new QRCode("qrcode", {
      //       width: 150, // 宽
      //       height: 150, // 高
      //       text: this.payData.QrCode, // 二维码内容,也可以直接放接口获取的数据
      //     });
      //     //添加定时器，检查订单是否支付成功
      //     let timer = setInterval(() => {
      //
      //     }, 5000);
      //   } else {
      //     this.$message.error("二维码生成失败")
      //   }
      // })
      // console.log(row)
    },
    // 作废
    handleInvalid(row) {
      this.$http.get("/hisRegistration/updateStatus/"+row.registrationId+"/"+"5").then(res => {
        if (res.data.code === 200) {
          this.$message.success("作废成功")
          this.listRegistration()
        } else {
          this.$message.error("作废失败")
        }
      })
    },
    // 退号
    handleReturn(row) {
      this.$http.get("/hisRegistration/updateStatus/"+row.registrationId+"/"+"4").then(res => {
        if (res.data.code === 200) {
          this.$message.success("退号成功")
          this.listRegistration()
        } else {
          this.$message.error("退号失败")
        }
      })
    }
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
